<?php
/**
 * @Author: Ron Chen
 * @Date:   2017-06-05 21:29:22
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2017-07-20 21:03:50
 */
namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;
use app\models\LogPayOrder;
use app\models\InsurancePolicy;
use app\services\PolicyServices;
use app\services\QiniuService;

/**
 * @name 异步通知借款     
 */
class NotifyController extends Controller{

	/**
	 * 异步回调通知
	 */
	public function actionOrder() {
		// 外网能访问的地址
//		Yii::$app->response->format = Response::FORMAT_JSON;
		$log = new LogPayOrder();
		$log->callback   = json_encode(Yii::$app->request->post());
		$log->created_at = time();
		$log->updated_at = time();
		$code = -1;
		if($log->save()){
			$code = 0;
		}

		return [
			"code" => $code,
			"message" => 'success'
		];
	}

	/**
	 * 打印合同
	 * @params 
	 */
	public function actionPrintOrder(){
		$order_id = Yii::$app->request->get('id');
		$policy_detail = InsurancePolicy::findOne($order_id);
		if (!$policy_detail) {
			return $this->redirect(['/index/list']);
		}
		
		$pay_service = new PolicyServices();
		
		$save_name = sprintf("/contract/%s.pdf",time());
		$save_data = $pay_service->printOrder($order_id);
		
		$oss = new QiniuService();
		$ret = $oss->uploadFile($save_name,$save_data);
		var_dump($ret);
		exit();

	}
}